<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
if(isset($_SESSION['lang']))
    require_once "./language_files/signUp2_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/signUp2_en.php";
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link href="style3.css" rel="stylesheet" type="text/css"/>
        <title></title>
    </head>
    <body>
        <button class="close" id="close2" onclick="javascript: top.window.location='index.php';"></button>
        <fieldset>
            <legend align="left"><?php echo $lang['sign'];?></legend>
            <table>
                <tr><th class="no_border_no_wrap" colspan="2"><b><?php echo $lang['info'];?></b></th></tr>
                <tr>
                    <p><?php echo $lang['hint'];?> </p>
                        <td class="no_border_no_wrap"><?php echo $lang['first'];?></td>
                        <td class="no_border_no_wrap"><input type="text" id="first_name" name="first_name"/></td>
                </tr>
                <tr>
                    <td class="no_border_no_wrap"><?php echo $lang['last'];?></td>
                    <td class="no_border_no_wrap"><input type="text" id="last_name" name="last_name"/></td>
                </tr>
                <tr>
                    <td class="no_border_no_wrap"><?php echo $lang['mail'];?></td>
                    <td class="no_border_no_wrap" ><input type="text" id="email" name="email" title="Please enter a valid email since you will need to confirm it before logging in"/><img id="mail_status" alt="email status ok" src="check.png" class="invisible"/><img id="wrong_mail" alt="wrong email" src="wrong.png" class="invisible"/></td>
                    <td class="no_border_no_wrap" ><label style="color:red;" id="email_label"></label></td>
                </tr>
                <tr>
                    <td class="no_border_no_wrap"><?php echo $lang['password'];?></td>
                    <td class="no_border_no_wrap"><input type="password" id="password" name="password"/><img id="password_status" alt="password status ok" src="check.png" class="invisible"/><img id="wrong_password" alt="wrong password" src="wrong.png" class="invisible"/></td>
                    <td class="no_border_no_wrap"><label style="color:red;" id="password_label"></label></td>
                </tr>
                <tr>
                    <td class="no_border_no_wrap"><?php echo $lang['confirmPass'];?></td>
                    <td class="no_border_no_wrap"><input type="password" id="confirm_password" name="confirm_password"/><img id="confirm_status" alt="confirm status" src="check.png" class="invisible"/><img id="wrong_confirm" alt="confirm error" src="wrong.png" class="invisible"/></td>
                    <td class="no_border_no_wrap"><label style="color:red;" id="confirm_label"></label></td>
                </tr>
                <tr>
                    <td class="no_border_no_wrap" colspan="3">
                        <?php
                        require_once "recaptchalib.php";
                        $public_key="6LdSNtMSAAAAAHx7RcBJWMigeODQdGiNvpULmAmt";
                        echo recaptcha_get_html($public_key);
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class="no_border_no_wrap"><button id="submit"><?php echo $lang['submit'];?></button></td>
                    <td class="no_border"><img id="loader" alt="loader" src="ajax-loader.gif" class="invisible"/></td>
                </tr>
            </table>
        </fieldset>
        <?php include "jquery.php";?>
        <script type="text/javascript">
            function enter(event)
            {
                if(event.keyCode==13)
                {
                    event.preventDefault();
                    $(".close").unbind("click");
                    $(event.target).blur();
                    $("#submit").trigger("click");
                    $(".close").bind("click",function()
                    {
                        top.window.location="index.php";
                    });
                    return false;
                }

            }
        </script>
        <script type="text/javascript">
            //<![CDATA[
            $(document).ready(function()
            {
                $("img").height($("#email").height());
                regexp=/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
                mail_ready=false;
                pass_ready=false;
                confirm_ready=false;
                $("#first_name,#last_name,#password,#confirm_password,#recaptcha_response_field").bind("focus",function()
                {
                    $(this).bind("keypress",enter);
                });
                $("#first_name,#last_name,#password,#confirm_password,#recaptcha_response_field").bind("blur",function()
                {
                    $(this).unbind("keypress");
                });
                $("#email").bind("blur",function()
                {
                    $("#email_label").html("");
                    val=$(this).val();
                    if(val.match(regexp)==null)
                    {
                        $(this).css("border", "2px red inset");
                        $("#wrong_mail").css("display", "inline");
                        $("#mail_status").css("display", "none");
                        mail_ready=false;
                    }
                    else
                    {
                        $.ajax({
                            type:"GET",
                            url:"signUpServer.php",
                            data:{unconfirmed_mail:$("#email").val()},
                            success:function(response)
                            {
                                if(response=="1")
                                {
                                    $("#email").css("border", "2px green inset");
                                    $("#mail_status").css("display", "inline");
                                    $("#wrong_mail").css("display", "none");
                                    mail_ready=true;
                                }
                                else if(response=="0")
                                {
                                    $("#email").css("border", "2px red inset");
                                    $("#mail_status").css("display", "none");
                                    $("#wrong_mail").css("display", "inline");
                                    $("#email_label").html("This email has been used before please choose another one");
                                    mail_ready=false;
                                }
                                else
                                    alert(response);
                            }
                        });
                    }
                });
                $("#password").bind("keyup input paste",function()
                {
                    $("#password_label").html("");
                    if($(this).val().length<8)
                    {
                        $(this).css("border", "2px red inset");
                        $("#wrong_password").css("display", "inline");
                        $("#password_status").css("display", "none");
                        $("#password_label").html("Password must be at least 8 characters long");
                        pass_ready=false;
                    }
                    else
                    {
                        $(this).css("border", "2px green inset");
                        $("#password_status").css("display", "inline");
                        $("#wrong_password").css("display", "none");
                        pass_ready=true;
                    }
                });
                $("#confirm_password,#password").bind("keyup input paste",function()
                {
                    $("#confirm_label").html("");
                    if($("#confirm_password").val()!=$("#password").val())
                    {
                        $("#confirm_password").css("border", "2px red inset");
                        $("#wrong_confirm").css("display", "inline");
                        $("#confirm_status").css("display", "none");
                        $("#confirm_label").html("Password and confirmation do not match");
                        confirm_ready=false;
                    }
                    else
                    {
                        if($(this).val()!="")
                        {
                            $("#confirm_password").css("border", "2px green inset");
                            $("#confirm_status").css("display", "inline");
                            $("#wrong_confirm").css("display", "none");
                            confirm_ready=true;
                        }
                    }
                });
                $("#submit").bind("click",function()
                {
                    if($("#first_name").val()=="" || $("#last_name").val()=="" || $("#email").val()=="" || $("#password").val()=="" || $("#confirm_password").val()=="")
                    {
                        alert("Please fill all required fields");
                        return;
                    }
                    if(!mail_ready || !pass_ready || !confirm_ready)
                    {
                        alert("please check that all input fields have valid values");
                        return;
                    }
                    $("#submit").attr("disabled", "disabled");
                    $("#recaptcha_response_field,#email,#password,#confirm_password").blur();
                    $("#loader").css("display", "inline");
                    $.ajax({
                        type:"POST",
                        url:"signUpServer.php",
                        data:{first:$("#first_name").val(),
                            last:$("#last_name").val(),
                            email:$("#email").val(),password:$("#password").val(),
                            confirm_password:$("#confirm_password").val(),
                            recaptcha_challenge_field:$("#recaptcha_challenge_field").val(),
                            recaptcha_response_field:$("#recaptcha_response_field").val()},
                        success:function(response)
                        {
                            if(response=="1")
                            {
                                $("#loader").css("display", "none");
                                alert("Your account has been registered successfully please check the confirmation mail for details on completing the registration");
                                window.location="signIn2.php";
                            }
                            else if(response.indexOf("Invalid recaptcha code")!=-1)
                            {
                                mail_ready=true;
                                pass_ready=true;
                                confirm_ready=true;
                                $("#submit").removeAttr("disabled");
                                $("#loader").css("display", "none");
                                alert(response);
                                Recaptcha.reload();
                            }
                            else
                            {
                                alert(response);
                                $("#submit").removeAttr("disabled");
                                $("#loader").css("display", "none");
                            }
                        }
                    });
                });
            });
            //]]>
        </script>
    </body>
</html>
